package com.tz.springmvc.sysmanage.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.support.RequestContext;

import com.tz.springmvc.sysmanage.entity.User;
import com.tz.springmvc.sysmanage.service.IUserService;

/**
 * 用于注解开发的LoginController
 * @author Administrator
 *
 */
@Controller
public class LoginController {
	 
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/gotoLogin")
	public String gotoLogin(){
		/*User user = null;
		user.getAge();*/
		/*System.out.println(request.getLocale());
 		RequestContext requestContext = new RequestContext(request);
		System.out.println(requestContext.getMessage("sysmanage.loginController.loginFail")); */
		return "login";
	}
	
	@RequestMapping("/login")
	public String login(@RequestParam(value="loginName" ,required=true) String loginName,
						String password,Model model,HttpSession session,HttpServletRequest request){	
		System.out.println("loginName is"+ loginName);
		System.out.println("password is "+ password);
		System.out.println("locale is "+ request.getLocale());

		//用redirect和forward标签返回
		//注意:用这两个标签必须写全路径,不适应视图解析的逻辑视图
		if(loginName!=null&&password!=null){
			if(userService.login(loginName, password)){
				session.setAttribute("loginName", loginName);
 				//相当于response.redirect(/main.jsp)
				return "redirect:/main.do";
			}else{
				model.addAttribute("loginFlag","sysmanage.loginController.loginFail");
				//相当于request.getDispatcher("/login.jsp").forward(request,response)
				return "forward:/login.jsp";
			}				
		}else{
			model.addAttribute("loginFlag","sysmanage.loginController.loginFail");
			return "forward:/login.jsp";
		}
		
	}
	
	@RequestMapping("/main")
	public String main(){
		return "main/main";
	}

}
